/**
 *@Description
 *@author your name
 *@createDate 2011-7-6 上午06:34:40
 *@version V1.0
 */
package com.welldone.service.offer;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.welldone.service.DBService;

public class OfferManager {

	/**
	 * 添加一个优惠
	 *@author your name
	 *@createDate 2011-7-6
	 *@param map
	 *@return
	 */
	public boolean addOffer(Map map){
		boolean flag = false;
		String sql = "insert into t_offer (offerId,offerName,offerStartDate,offerEndDate,discount,isVip,discountVip,offerStatus) values(?,?,?,?,?,?,?,?)";
		String offerId = UUID.randomUUID().toString();
		Object args[] = {offerId,map.get("offerName"),map.get("offerStartDate"),map.get("offerEndDate"),map.get("discount"),map.get("isVip"),map.get("discountVip"),map.get("offerStatus")};
		int result = DBService.getJdbcTemplate().update(sql,args);
		if(result !=-1){
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 更新一个优惠
	 *@author your name
	 *@createDate 2011-7-6
	 *@param map
	 *@return
	 */
	public boolean updateOffer(Map map){
		boolean flag = false;
		String sql = "update t_offer  set offerName=?,offerStartDate=?,offerEndDate=?,discount=?,isVip=?,discountVip=?,offerStatus=? where offerId = ?";
		Object args[] = {map.get("offerName"),map.get("offerStartDate"),map.get("offerEndDate"),map.get("discount"),map.get("isVip"),map.get("discountVip"),map.get("offerStatus"),map.get("offerId")};
		int result = DBService.getJdbcTemplate().update(sql,args);
		if(result !=-1){
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 删除一个优惠
	 *@author your name
	 *@createDate 2011-7-6
	 *@param map
	 *@return
	 */
	public boolean deleteOffer(String offerId){
		boolean flag = false;
		String sql = "delete from t_offer where offerId = ?";
		Object args[] = {offerId};
		int result = DBService.getJdbcTemplate().update(sql,args);
		if(result !=-1){
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 查询所有的优惠
	 *@author your name
	 *@createDate 2011-7-6
	 *@return
	 */
	public List getAllOffer(){
		String sql = "select * from t_offer";
		return DBService.getJdbcTemplate().queryForList(sql);
	}
	
	/**
	 * 根据时间查询优惠
	 *@author your name
	 *@createDate 2011-7-6
	 *@param startTime
	 *@param endTime
	 *@return
	 */
	public List getOfferByDate(String startTime,String endTime){
		String sql = "select * from t_offer where offerStartDate>=? and offerStartDate<=? or offerEndDate>=? and offerEndDate<=? and offerStatus = 1";
		Object[] args = {startTime,startTime,endTime,endTime};
		return DBService.getJdbcTemplate().queryForList(sql);
	}
	
	/**
	 * 查询当前可用的优惠
	 *@author your name
	 *@createDate 2011-7-7
	 *@return
	 */
	public List getOfferByNow(){
		String sql = "select * from t_offer where offerStatus='1'";
		return DBService.getJdbcTemplate().queryForList(sql);
	}
	
	
	
}


